package spring.cloud.tasks.executor.name_space.executor.task.task_listener.trigger_task_execute;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.node.ExecutorNodePath;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.executor.name_space.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.TaskListenerHolder;

/**
 * 任务控制监听管理器.
 */
@Slf4j
@Setter
@Getter
public class TriggerTaskExecuteListenerHolder extends TaskListenerHolder {

    public boolean isShutdown = false;

    public TriggerTaskExecuteListenerHolder(final TaskExecutor taskExecutor) {
        super(taskExecutor);
    }

    @Override
    public void start() {
        taskExecutorNodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePath.getTaskNodePath(taskId, String.format(ExecutorNodePath.EXECUTOR_RUN_ONE_TIME, executorId)), 0, new TriggerTaskRunAtOnceListener(this));
        taskExecutorNodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePath.getTaskNodePath(taskId, String.format(ExecutorNodePath.EXECUTOR_STOP_ONE_TIME, executorId)), 0, new TriggerTaskStopAtOnceListener(this));

    }

    @Override
    public void stop() {
        super.stop();
        isShutdown = true;
        taskExecutorNodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePath.getTaskNodePath(taskId, String.format(ExecutorNodePath.EXECUTOR_RUN_ONE_TIME, executorId)), 0);
        taskExecutorNodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePath.getTaskNodePath(taskId, String.format(ExecutorNodePath.EXECUTOR_STOP_ONE_TIME, executorId)), 0);

    }

}
